package oss

import (
	"fmt"
	"mime/multipart"
	"path/filepath"
	"time"

	"admin.supervise.limaochuxing.top/admin/schemas/resp"
	"admin.supervise.limaochuxing.top/util"
	"admin.supervise.limaochuxing.top/util/oss"

	"gorm.io/gorm"
)

type IOssService interface {
	UploadFile(file *multipart.FileHeader) (*resp.OssUploadFileResp,error)
}

type ossService struct {
	db *gorm.DB
}

func NewOssService(db *gorm.DB) IOssService {
	return &ossService{db: db}
}

func (s *ossService) UploadFile(file *multipart.FileHeader) (*resp.OssUploadFileResp,error)   {
	ossManager:=oss.NewOssManager()
	ext:=filepath.Ext(file.Filename)
	filePath:=fmt.Sprintf("file/%s/%s/%s/%s%s",
	time.Now().Format("2006"),
	time.Now().Format("01"),
	time.Now().Format("02"),
	util.ToolsUtil.MakeMd5(time.Now().String()),
	ext)
	url,err:= ossManager.UploadFile(file,filePath)
	if err!=nil{
		return nil,err
	}
	fmt.Println(url)
	return &resp.OssUploadFileResp{
		Url:url,
	},nil
}
